/* Copyright 2012 Dustin Mitchell <dmmitche [at] gmail [dot] com>
 *
 * Licensed under the GPLv3.  See included LICENSE.txt for full details.
 */

#ifndef __REF_CNT__
#define __REF_CNT__

#include <pthread.h>

struct ref_cnt
{
  pthread_mutex_t sync;
  size_t cnt;
  void (*destroy) (void *);
  void (*self_destroy) (void *);
  void *obj;
};

int ref_cnt_create (struct ref_cnt *ref, void *obj, void (*destroy) (void *),
  void (*self_destroy) (void *));

void ref_cnt_destroy (struct ref_cnt *ref);

void * ref_cnt_get_obj (struct ref_cnt *ref);

int ref_cnt_ref (struct ref_cnt *ref);

int ref_cnt_deref (struct ref_cnt *ref);

#define REF_CNT(p) ((struct ref_cnt *)(p))

#endif /* __REF_CNT__ */
